﻿///<reference path="../jquery/jquery.js"/>
///<reference path="../plugins/jquery.srollTo.js"/>
var CategoryList = function() {
	//private area
	var _ajaxDefaultOpts = {
		type: 'post',
		dataType: 'json',
		contentType: 'application/json;charset=utf-8'
	};
	//public areas
	return {
		InitPager: "",
		Asmx: "",
		FatherID: "",
		ViewPath: "~/Views/CategoryListView.ascx",
		Init: function(opts) {
			CategoryList.Asmx = opts.Asmx || "";
			CategoryList.FatherID = opts.FatherID || "";
			CategoryList.InitPager = opts.InitPagerStr || "";
			CategoryList.ViewPath = opts.ViewPath || CategoryList.ViewPath;
			//Do first load.
			CategoryList.Select($.evalJSON(CategoryList.InitPager));
		}, //endof Init
		DefaultDialogOpts: {
			modal: true,
			width: 600,
			height: 400,
			resizable: false,
			overlay: { opacity: 0.5, background: 'black' }
		},
		Select: function(opts) {
			var data = {};
			data.CPage = opts.CurrentPage;
			data.RPPage = opts.RowPerPage;
			data.FatherID = CategoryList.FatherID;
			data.ViewPath = CategoryList.ViewPath;
			var ajaxOpts = _ajaxDefaultOpts;
			ajaxOpts.url = CategoryList.Asmx + "SelectCategories";
			ajaxOpts.success = function(msg) {
				msg = msg.d;
				if (msg.IsOk) {
					//Replace CategoryList
					$("#categoriesWrapper").html(msg.Context.CategoryListHtml);
					//Replace pager
					$("#pagerWrapper").html(msg.Context.PagerHtml);
					//Init pager again
					SimplePager.Init(msg.Context.Pager, CategoryList.Select);
					//scroll to the latest record
					if (msg.Context.TopItemID) {
						var topItemDivID = "id_Category" + msg.Context.TopItemID; //topItemID was determined by CategoryView.ascx.
						$.scrollTo("#"+topItemDivID);
					}
				} else {
					alert("Something goes wrong!");
				}
			};
			ajaxOpts.data = $.toJSON({ Params: data });
			$.ajax(ajaxOpts);
		} //endof Select
	};
} ();